package com.flt.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.flt.dao.CustomMenuDAO;
import com.flt.dao.MenuDAO;
import com.flt.dao.ProductDAO;
import com.flt.dao.model.Menu;
import com.flt.dao.model.MenuExample;
import com.flt.dao.model.Product;
import com.flt.dao.model.ProductExample;
import com.flt.module.homepage.service.IHomepageService;

@Service
public class HomepageService implements IHomepageService {

	@Resource
	private MenuDAO menuDAOImpl;
	
	@Resource
	private CustomMenuDAO customMenuDAO;
	
	@Resource
	private ProductDAO productDAOImpl;
	
	@Override
	public List<Menu> findAllMenus() {
		// TODO Auto-generated method stub
		return menuDAOImpl.selectByExample(new MenuExample());
	}

	@Override
	public Menu findDefaultMenu() {
		// TODO Auto-generated method stub
		
		final Menu menu=customMenuDAO.findMenuByMinId();
		
		return menu;
	}

	@Override
	public Menu findMenuById(Integer menuId) {
		// TODO Auto-generated method stub
		return menuDAOImpl.selectByPrimaryKey(menuId);
	}

	@Override
	public List<Product> findProductsByMenuId(final Integer menuId, Integer sortby) {
		// TODO Auto-generated method stub
		
		List list=null;
		if(sortby==null){
			list=productDAOImpl.selectByExampleWithBLOBs(new ProductExample(){{
				this.createCriteria().andMenuIdEqualTo(menuId);
			}});
			
			return list;
		}
		
		String orderby=null;
		
		switch(sortby){
		case 1:{
			orderby="ct_time DESC";
			break;
		}
		case 2:{
			orderby="sale_count DESC";
			break;
		}
		case 3:{
			orderby="grade DESC";
			break;
		}
		default :{
			orderby="no DESC";
		}
		}
		
		final String or=orderby;
		list=productDAOImpl.selectByExampleWithBLOBs(new ProductExample(){{
			this.createCriteria().andMenuIdEqualTo(menuId);
			this.setOrderByClause(or);
		}});
		
		return list;
	}

}
